package com.budwk.app.sqfwV2.generator.weiqingmen;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.weiqingmen.RectifyDTO;
import com.budwk.app.sqfwV2.dto.weiqingmen.SafetyCheckDTO;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.PictureUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-04-01
 * <p>
 * 威清门消防安全排查 SafetyCheck
 * 餐饮经营服务场所消防安全巡查台账.docx
 */
public class CateringServicePdfGenerator extends BasePdfGenerator {

    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfDTO.getAbstractCustomerPdfCreateField();
        boolean createFlag = false;
        if (fieldDTO.getAttributesOfInspectionUnits().equals("餐饮店面")) {
            createFlag = true;
        }
        return createFlag;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "sqfwV2/weiqingmen/CateringService.docx";
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {

        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        List<RectifyDTO> recordsDTO = fieldDTO.getArrayOfFireSafetyPatrolRecords();

        // 设置巡查内容状态
        BaseGenerator.setArrayOfFireSafetyPatrolRecords(paramMap, recordsDTO);

        // 巡查时间
        Date inspectionDate = fieldDTO.getInspectionDate();
        paramMap.put("yyyy", DateUtil.year(inspectionDate));
        paramMap.put("mm", DateUtil.month(inspectionDate) + 1);//月份从0开始,所以要加1
        paramMap.put("dd", DateUtil.dayOfMonth(inspectionDate));

        // 现场整改
        List<String> sceneChange = BaseGenerator.getSceneChangeNum(recordsDTO);
        paramMap.put("sceneChange", StringUtils.join(sceneChange, "、"));
        // 限期整改
        List<String> deadlineChange = BaseGenerator.getDeadlineChangeNum(recordsDTO);
        paramMap.put("deadlineChange", StringUtils.join(deadlineChange, "、"));

        // 限期整改时间
        Date deadlineForRectification = fieldDTO.getDeadlineForRectification();
        paramMap.put("cmm", deadlineChange.size() > 0 ? DateUtil.month(deadlineForRectification) + 1 : "");//月份从0开始,所以要加1
        paramMap.put("cdd", deadlineChange.size() > 0 ? DateUtil.dayOfMonth(deadlineForRectification) : "");

        // 创建人的信息
        CommonParam createUserCommonParam = pdfGenerateParam.getCreateUserCommonParam();
        if (StringUtils.isNotBlank(createUserCommonParam.getCreateUserSigUrl())) {
            paramMap.put(ClassUtil.getField(CommonParam::getCreateUserName), "");
        }

        // 复查情况
        paramMap.put("omm", "");
        paramMap.put("odd", "");
        paramMap.put("change", "");
        paramMap.put("notChange", "");
        paramMap.put("oUserName", "");
        Sqfw_formSummary_new lastFormSummary = (Sqfw_formSummary_new) pdfGenerateParam.getCustomerParamMap().get("lastFormSummary");
        // 不为空，表示存在上一次巡查，进入复查
        if (lastFormSummary != null) {
            SafetyCheckDTO dto = JSONObject.parseObject(lastFormSummary.getParamData(), SafetyCheckDTO.class);
            String hidden = BaseGenerator.getHiddenNotOther(dto, "限期整改");
            // 当上一次存在限期整改问题时才需要复查
            if ("无".equals(hidden)) return;

            List<RectifyDTO> patrolRecordsDTO = dto.getArrayOfFireSafetyPatrolRecords();
            // 巡查时间
            paramMap.put("omm", DateUtil.month(fieldDTO.getInspectionDate()) + 1);//月份从0开始,所以要加1
            paramMap.put("odd", DateUtil.dayOfMonth(fieldDTO.getInspectionDate()));

            // 获取上次巡查限时整改问题
            List<String> lastDeadlineChange = BaseGenerator.getDeadlineChangeNum(patrolRecordsDTO);

            // 获取已整改问题
            List<String> changeNum = BaseGenerator.getChangeNum(recordsDTO);
            paramMap.put("change", StringUtils.join(changeNum, "、"));

            //获取未整改问题(交集)
            List<String> scene_deadline_ChangeNum = sceneChange;
            scene_deadline_ChangeNum.addAll(deadlineChange);
            Collection<String> notChange = CollectionUtils.intersection(lastDeadlineChange, scene_deadline_ChangeNum);
            paramMap.put("notChange", StringUtils.join(notChange, "、"));

            // 复查人的信息
            if (StringUtils.isNotBlank(createUserCommonParam.getCreateUserSigUrl())) {
                paramMap.put("oUserName", "");
            } else {
                paramMap.put("oUserName", createUserCommonParam.getCreateUserName());
            }
        }
    }

    /**
     * 签名、盖章
     * @param xwpfTemplate         XWPDF文件
     * @param pdfGenerateParam 生成pdf的参数
     */
    protected void createXWPDFTemplateAfter(XWPFTemplate xwpfTemplate, PdfGenerateParam pdfGenerateParam) {
        Sqfw_formSummary_new lastFormSummary = (Sqfw_formSummary_new) pdfGenerateParam.getCustomerParamMap().get("lastFormSummary");
        CommonParam createUserCommonParam = pdfGenerateParam.getCreateUserCommonParam();

        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();

        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "被巡查单位人员签名", fieldDTO.getCheckedUnitLeader(),
                    140, -10, 65);
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "巡查人员签名", createUserCommonParam.getCreateUserSigUrl(),
                    95, -10, 65);
            // 判断是否存在复查
            if (lastFormSummary != null) {
                SafetyCheckDTO dto = JSONObject.parseObject(lastFormSummary.getParamData(), SafetyCheckDTO.class);
                String hidden = BaseGenerator.getHiddenNotOther(dto, "限期整改");

                if ("无".equals(hidden)) return;

                xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "被复查单位人员签名", fieldDTO.getCheckedUnitLeader(),
                        140, -10, 65);
                PictureUtils.networkImageSeal(xwpfDocument, "复查人员签名", createUserCommonParam.getCreateUserSigUrl(),
                        95, -10, 65);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
